package com.kehao.accountbook.fragment;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kehao.accountbook.AccountApplication;
import com.kehao.accountbook.ChangePasswordActivity;
import com.kehao.accountbook.LoginActivity;
import com.kehao.accountbook.MainActivity;
import com.kehao.accountbook.R;


public class SettingFragment extends Fragment {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    TextView tv_username;
    CheckBox cb_rempwd;
    Button btn_quit;
    LinearLayout btn_change_password;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_setting, container, false);
        tv_username = view.findViewById(R.id.tv_username);
        tv_username.setText("用户名 ： "+getAccountApplication().getUsername());
        cb_rempwd =view.findViewById(R.id.cb_rempwd);
        cb_rempwd.setChecked(getAccountApplication().isRempwd());
        cb_rempwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getAccountApplication().isRempwd()){
                    getAccountApplication().setRempwd(false);
                }else {
                    getAccountApplication().setRempwd(true);
                }
                sharedPreferences = getActivity().getSharedPreferences("data", Activity.MODE_PRIVATE);
                editor = sharedPreferences.edit();
                editor.putBoolean("rempwd",getAccountApplication().isRempwd());
                editor.putString("username",getAccountApplication().getUsername());
                editor.putString("password",getAccountApplication().getPassword());
                editor.apply();
            }
        });
        btn_quit = view.findViewById(R.id.btn_quit);
        btn_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
        btn_change_password = view.findViewById(R.id.btn_change_password);
        btn_change_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ChangePasswordActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }


    private AccountApplication getAccountApplication(){
        return (AccountApplication)getActivity().getApplication();
    }

}
